<template>
  <div class="product-template">
    <h3>选择商品模板</h3>
    <p class="tip">选择合适的商品展示模板</p>
    
    <div class="template-list">
      <el-row :gutter="20">
        <el-col
          v-for="template in displayedTemplates"
          :key="template.id"
          :span="6"
        >
          <div
            class="template-item"
            :class="{ active: selectedTemplate && selectedTemplate.id === template.id }"
            @click="selectTemplate(template)"
          >
            <div class="template-image">
              <img :src="template.image" :alt="template.name">
              <div v-if="selectedTemplate && selectedTemplate.id === template.id" class="selected-icon">
                <el-icon><Check /></el-icon>
              </div>
            </div>
            <div class="template-info">
              <div class="template-name">{{ template.name }}</div>
              <div class="template-type">
                <el-tag size="small">{{ template.type }}</el-tag>
              </div>
              <div class="template-desc">{{ template.description }}</div>
            </div>
          </div>
        </el-col>
      </el-row>

      <!-- 空状态 -->
      <el-empty v-if="displayedTemplates.length === 0" description="暂无模板数据" />
    </div>

    <!-- 分页 -->
    <div class="pagination-wrapper">
      <el-pagination
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[4, 8, 12, 16]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { ref, computed, watch } from 'vue'
import { Check } from '@element-plus/icons-vue'
import { templateData } from '../../utils/productMock'

export default {
  name: 'ProductTemplate',
  components: {
    Check
  },
  props: {
    modelValue: {
      type: Object,
      default: null
    }
  },
  emits: ['update:modelValue'],
  setup(props, { emit }) {
    const templates = ref(templateData)
    const selectedTemplate = ref(null)
    const currentPage = ref(1)
    const pageSize = ref(4)

    const total = computed(() => templates.value.length)

    const displayedTemplates = computed(() => {
      const start = (currentPage.value - 1) * pageSize.value
      const end = start + pageSize.value
      return templates.value.slice(start, end)
    })

    // 监听props变化
    watch(() => props.modelValue, (newVal) => {
      selectedTemplate.value = newVal
    }, { immediate: true })

    // 选择模板
    const selectTemplate = (template) => {
      selectedTemplate.value = template
      emit('update:modelValue', template)
    }

    // 分页大小改变
    const handleSizeChange = (newSize) => {
      pageSize.value = newSize
      currentPage.value = 1
    }

    // 当前页改变
    const handleCurrentChange = (newPage) => {
      currentPage.value = newPage
    }

    return {
      templates,
      selectedTemplate,
      currentPage,
      pageSize,
      total,
      displayedTemplates,
      selectTemplate,
      handleSizeChange,
      handleCurrentChange
    }
  }
}
</script>

<style scoped>
.product-template {
  padding: 20px;
}

.product-template h3 {
  margin: 0 0 10px 0;
  font-size: 18px;
  color: #303133;
}

.tip {
  margin: 0 0 20px 0;
  color: #909399;
  font-size: 14px;
}

.template-list {
  min-height: 400px;
  margin-bottom: 20px;
}

.template-item {
  cursor: pointer;
  border: 2px solid #dcdfe6;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s;
  margin-bottom: 20px;
}

.template-item:hover {
  border-color: #409eff;
  box-shadow: 0 2px 12px 0 rgba(64, 158, 255, 0.3);
  transform: translateY(-2px);
}

.template-item.active {
  border-color: #409eff;
  box-shadow: 0 2px 12px 0 rgba(64, 158, 255, 0.5);
}

.template-image {
  position: relative;
  width: 100%;
  height: 180px;
  overflow: hidden;
  background-color: #f5f7fa;
}

.template-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.selected-icon {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 30px;
  height: 30px;
  background-color: #409eff;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 16px;
}

.template-info {
  padding: 15px;
}

.template-name {
  font-size: 16px;
  font-weight: 500;
  color: #303133;
  margin-bottom: 8px;
}

.template-type {
  margin-bottom: 8px;
}

.template-desc {
  font-size: 12px;
  color: #909399;
  line-height: 1.5;
}

.pagination-wrapper {
  display: flex;
  justify-content: center;
  margin-top: 20px;
}
</style>
